<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="核销码" prop="verificationCode">
        <el-input
          v-model="queryParams.verificationCode"
          placeholder="请输入核销码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系方式" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入联系方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['shop/reservation:reservation:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="reservationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="预约编号" align="center" prop="id" width="80" />
      <el-table-column label="车主姓名" align="center" prop="name" width="100" />
      <el-table-column label="联系方式" align="center" prop="phone" width="120" />
      <el-table-column label="车牌号" align="center" prop="carNum" width="100" />
      <el-table-column label="维修方式" align="center" prop="type" width="100">
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.type === '0' ? 'success' : 'danger'"
            effect="dark"
            size="medium">
            <i :class="scope.row.type === '0' ? 'el-icon-position' : 'el-icon-truck'"></i>
            {{ scope.row.type === '0' ? '到店维修' : '上门取车' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="预约时间" align="center" prop="reservationTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reservationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="提交时间" align="center" prop="submitTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.submitTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="维修状态" align="center" prop="status" min-width="100">
        <template slot-scope="scope">
          <el-tag
            :type="getStatusType(scope.row.status)"
            :effect="scope.row.status === '0' ? 'dark' : 'plain'"
            size="medium">
            <i :class="getStatusIcon(scope.row.status)"></i>
            {{ getStatusText(scope.row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="取车位置" align="center" prop="location" min-width="150">
        <template slot-scope="scope">
          <el-tooltip :content="scope.row.location" placement="top" effect="light">
            <span>{{ formatLocation(scope.row.location) }}</span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="店铺" align="center" prop="shop.name" width="80" />
      <el-table-column label="用户姓名" align="center" prop="user.name" width="100" />
      <el-table-column label="车型" align="center" prop="carType.name" width="80" />
      <el-table-column label="核销码" align="center" prop="verificationCode" width="180" />
      <el-table-column 
        v-if="isAdmin"
        label="核销码图片" 
        align="center" 
        width="120">
        <template slot-scope="scope">
          <el-image
            style="width: 50px; height: 50px"
            :src="scope.row.verificationImg"
            :preview-src-list="[scope.row.verificationImg]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column 
        label="全车照片" 
        align="center"
        width="120">
        <template slot-scope="scope">
          <el-image
            v-if="scope.row.vehiclePhotos"
            style="width: 50px; height: 50px"
            :src="scope.row.vehiclePhotos"
            :preview-src-list="[scope.row.vehiclePhotos]">
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column 
        label="维修部位照片" 
        align="center"
        width="120">
        <template slot-scope="scope">
          <el-image
            v-if="scope.row.repairPhotos"
            style="width: 50px; height: 50px"
            :src="scope.row.repairPhotos"
            :preview-src-list="[scope.row.repairPhotos]">
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listReservation, getReservation } from "@/api/shop/reservation/reservation";

export default {
  name: "Reservation",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 维修预约表格数据
      reservationList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        verificationCode: null,
        phone: null
      },
      isAdmin: false,
    };
  },
  created() {
    this.getList();
    this.checkUserRole();
  },
  methods: {
    /** 查询维修预约列表 */
    getList() {
      this.loading = true;
      listReservation(this.queryParams).then(response => {
        this.reservationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('shop/reservation/reservation/export', {
        ...this.queryParams
      }, `reservation_${new Date().getTime()}.xlsx`)
    },
    /** 检查用户角色 */
    checkUserRole() {
      const roles = this.$store.getters && this.$store.getters.roles;
      this.isAdmin = roles.includes('admin') || roles.includes('superadmin');
    },
    /** 获取状态类型 */
    getStatusType(status) {
      const types = {
        '0': 'warning',   // 已预约
        '1': 'primary',   // 进行中
        '2': 'success',   // 已完成
        '3': 'danger'     // 修改取消状态的样式为红色
      }
      return types[status] || 'info'
    },
    /** 获取状态图标 */
    getStatusIcon(status) {
      const icons = {
        '0': 'el-icon-time',      // 已预约
        '1': 'el-icon-loading',   // 进行中
        '2': 'el-icon-check',     // 已完成
        '3': 'el-icon-circle-close'  // 修改取消状态的图标
      }
      return icons[status] || 'el-icon-info'
    },
    /** 获取状态文本 */
    getStatusText(status) {
      const texts = {
        '0': '已预约',
        '1': '进行中',
        '2': '已完成',
        '3': '订单已取消'  // 修改这里的文本
      }
      return texts[status] || '未知状态'
    },
    /** 格式化位置信息 */
    formatLocation(location) {
      if (!location) return '暂无位置信息';
      // 只显示|前面的部分
      const parts = location.split('|');
      return parts[0] || location;
    },
  }
};
</script>

<style scoped>
.el-table .cell {
  white-space: nowrap;
}
</style>
